package com.qf.service.imp;

import com.qf.mapper.ClassMapper;
import com.qf.mapper.StudentMapper;
import com.qf.mapper.StudentMapper2;
import com.qf.mapper.SubMapper;
import com.qf.model.Classroom;
import com.qf.model.Student;
import com.qf.model.StudentVO;
import com.qf.model.Subject;
import com.qf.service.StudentService2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * --- 天道酬勤 ---
 *
 * @author GUOFUCHENG
 * @date 2025/8/14
 * @desc
 */
@Service
public class StudentService2Impl implements StudentService2 {
    @Autowired
    private StudentMapper2 studentMapper2;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private ClassMapper classMapper;
    @Autowired
    private SubMapper subMapper;

    @Override
    public List<StudentVO> findAllStuAndCal() {
        return studentMapper2.findAllStuAndCal();
    }

    @Override
    public List<StudentVO> findStuAndSubInfo(int id) {
        return studentMapper2.findStuAndSubInfo(id);
    }

    @Override
    public List<StudentVO> findStuAndSubAndTeaInfo(int id) {
        return studentMapper2.findStuAndSubAndTeaInfo(id);
    }

    @Override
    public List<StudentVO> findAllStudentAndClassroomV2() {
        ArrayList<StudentVO> list = new ArrayList<>();
        System.out.println("list = " + list);
        List<Student> studentList = studentMapper.findAll();
        studentList.forEach(student -> {
            Classroom classroom = classMapper.findClassroomById(student.getCid());
            StudentVO studentVO = new StudentVO();
            BeanUtils.copyProperties(student, studentVO);
            studentVO.setClassroom(classroom);
            list.add(studentVO);
            System.out.println("studentList = " + studentList);
        });
        return list;
    }

    @Override
    public StudentVO findStudentAndAllSubjectByStuidV2(int id) {
        Student student = studentMapper.findByid(id);
        System.out.println("student = " + student);
        // 创建集合准备存储每个学科对象
        List<Subject> subjectList = new ArrayList<>(  );
        // 再根据学生id查出关联表中所关联的所有学科id
        List<Integer> subIdList = studentMapper.findAllSubjectId(id);
        // 遍历所有id
        subIdList.forEach(subId -> {
            // 再根据学科id获得每一个学科对象
            Subject subject = subMapper.findSubjectById(subId);
            // 存储学科对象
            subjectList.add(subject);
        });

        // 封装数据到VO
        StudentVO studentVO = new StudentVO( );
        BeanUtils.copyProperties(student,studentVO);
        studentVO.setSubjectList(subjectList);
        return studentVO;
    }
}
